/* jshint esversion: 6 */
const Router = require('koa-router');
const tool = require("../../tools/tool");
const router = new Router();
const {db, Op, QueryTypes} = require("../../models/db");
// 修改密码
router.post('/user/dataAuth', async ctx => {
    let allData = ctx.request.body.allData
    let selfData = ctx.request.body.selfData
    let userId = ctx.request.body.userId
    let tid = ctx.user.tenantId;
    let owner = ctx.request.body.owner
    let dept = ctx.request.body.dept
    if(!userId) {
        return ctx.err('未发现用户')
    }
    await db.transaction(async () => {
        await db.models['admin'].update({
            allData: allData?1:0,
            selfData: selfData?1:0
        }, {
            where: {
                id: userId
            }
        }, )
        await db.models['adminDataOwer'].destroy({
            where: {
                selfId: userId
            }
        }, )
        await db.models['adminDataDept'].destroy({
            where: {
                selfId: userId
            }
        }, )
        if(owner.length > 0) {
            let ow = []
            owner.map(row => {
                ow.push({selfId: userId, owerId: row.id, tenantId: tid})
            })
            await db.models['adminDataOwer'].bulkCreate(ow, )
        }
        if(dept.length > 0) {
            let dp = []
            for (let i = 0; i < dept.length; i++) {
                dp.push({selfId: userId, deptId: dept[i], tenantId: tid})
            }
            await db.models['adminDataDept'].bulkCreate(dp, )
        }
    })
    return ctx.suc('更新成功')
})


module.exports = router;